package com.music.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.mapper.SingerMapper;
import com.music.mapper.SingleSongMapper;
import com.music.pojo.Album;
import com.music.pojo.Singer;
import com.music.pojo.SingleSong;
import com.music.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SingerServiceImpl implements SingerService {
    @Autowired
    private SingerMapper singerMapper;

    @Autowired
    private SingleSongMapper singleSongMapper;
    @Override
    public PageInfo<Singer> findSingerBySearch(String search, Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<Singer> singerBySearch  = singerMapper.findSingerBySearch(search);
        PageInfo<Singer> pageInfo = new PageInfo<>(singerBySearch);
        return pageInfo;
    }

    @Override
    public Integer isFollow(Integer sid, Integer uid) {
        return singerMapper.isFollow(sid,uid);
    }

    @Override
    public Singer findSingerWithAlbum(Integer sid,Integer uid) {
        if (uid==null){
            return singerMapper.findSingerWithAlbum(sid);
        }else {
            Singer singer = singerMapper.findSingerWithAlbum(sid);
            for (Album album : singer.getAlbums()) {
                for (SingleSong song : album.getSongs()) {
                    Integer num = singleSongMapper.isFavorite(song.getMid(), uid);
                    song.setFavoriteStatus(num);
                }
            }
            for (SingleSong singleSong : singer.getSingleSongs()) {
                Integer num = singleSongMapper.isFavorite(singleSong.getMid(), uid);
                singleSong.setFavoriteStatus(num);
            }
            return singer;
        }
    }

}
